<template>
	<view class="detail">
		<view class="title">
			{{detail.name}}
			<!-- 判断用户是否报名，显示对应的按钮文字 -->
			<button size="mini" class="join" @click="join">{{this.isEntry}}</button>
		</view>
		
		<view class="info">
			<view>发布时间：{{detail.publishTime}}</view>
			<view>报名人数：{{detail.signupNum+this.add}}</view>
		</view>
		
		<view class="content">
			<rich-text :nodes="detail.content"></rich-text>
		</view>
		
		<view class="recom">
			<view class="recomTop">活动推荐</view>
			<!-- 活动/新闻列表组件 -->
			<view v-for="item in actionList" :key="item.id" class="recomList">
				<ArticleList 
				:article="item" 
				goDetailsUrl="/pages/activity/detail?id="></ArticleList>
			</view>
		</view>
		
		<view class="commentList">
			<view class="comNum" v-if="this.comNum!=0">评论({{this.comNum}}) ：</view>
			<view v-else class="noCom">暂无评论，快来发表第一条评论吧！</view>
			<view v-for="item in comList" :key="item.id" class="comList">
				<text class="name">{{item.userName}}：</text>{{item.content}}
				<view class="time">{{item.commentTime}}</view>
			</view>
			<view v-if="this.comNum>5" class="allcom" @click="goComment">查看所有评论</view>
		</view>
		
		<view class="comment">
			<input type="text" placeholder="发表一条精彩的评论" v-model="com">
			<button size="mini" :disabled="com!=''? false:true" @click="sendCom">发表</button>
		</view>
		
		<!-- 有高度的空内容，防止评论框遮挡 -->
		<view class="no"></view>
	</view>
</template>

<script>
	import {http} from "../../utils/request.js"
	export default {
		data() {
			return {
				id:0,
				title:"",
				isEntry:"",
				add:0,
				detail:{},
				actionList:[],
				comNum:0,
				comList:[],
				com:""
			}
		},
		onLoad(e) {
			this.id=e.id
			this.getdetail()
			this.judgeJoin()
			this.getList()
			this.getComnum()
			this.getComlist()
		},
		methods: {
			// 报名判断
			judgeJoin(){
				// 判断用户是否报名活动
				http({
					url:"/prod-api/api/activity/signup/check",
					data:{
						activityId:this.id
					}
				}).then(res=>{
					if(res.data.code==200){
						if(res.data.msg=="已报名"){
							this.isEntry="已报名"
						}else{
							this.isEntry="报名"
						}
					}else{
						uni.showToast({
							title:res.data.msg,
							icon:"none"
						})
					}
				})
			},
			// 报名
			join(){
				if(this.isEntry=='已报名'){
					uni.showToast({
						title:"你已经报名过了！",
						icon:"none"
					})
				}else{
					http({
						url:"/prod-api/api/activity/signup",
						method:"POST",
						data:{
							activityId:this.id
						}
					}).then(res=>{
						if(res.data.code==200){
							uni.showToast({
								title:"报名成功！"
							})
							this.isEntry='已报名'
							this.add=1
						}else{
							uni.showToast({
								title:res.data.msg,
								icon:"none"
							})
						}
					})
				}
			},
			// 获取活动内容
			getdetail(){
				uni.showLoading({
					title:"数据加载中..."
				})
				http({
					url:"/prod-api/api/activity/activity/"+this.id,
				}).then(res=>{
					if(res.data.code==200){
						this.detail=res.data.data
						// 动态设置标题栏文字
						this.title=this.detail.name
						uni.setNavigationBarTitle({
							title:this.detail.name
						})
						uni.hideLoading()
					}else{
						uni.showToast({
							title: res.data.msg,
							icon:"none"
						})
					}
				})
			},
			// 获取推荐活动列表
			getList(){
				http({
					url:"/prod-api/api/activity/activity/list",
					data:{
						recommend:"Y"
					}
				}).then(res=>{
					if(res.data.code==200){
						this.actionList=res.data.rows
						// 随机截取出前面1-3个推荐内容
						this.actionList=this.actionList.slice(0,Math.floor(Math.random()*3+1))
					}else{
						uni.showToast({
							title: res.data.msg,
							icon:"none"
						})
					}
				})
			},
			// 获取活动评论数
			getComnum(){
				http({
					url:"/prod-api/api/activity/comment/number",
					data:{
						activityId:this.id
					}
				}).then(res=>{
					if(res.data.code==200){
						this.comNum=res.data.commentNum
					}else{
						uni.showToast({
							title: res.data.msg,
							icon:"none"
						})
					}
				})
			},
			// 获取活动评论列表
			getComlist(){
				http({
					url:"/prod-api/api/activity/comment/list",
					data:{
						activityId:this.id
					}
				}).then(res=>{
					if(res.data.code==200){
						this.comList=res.data.rows.slice(0,5)
					}else{
						uni.showToast({
							title: res.data.msg,
							icon:"none"
						})
					}
				})
			},
			// 跳转到活动评论页
			goComment(){
				uni.navigateTo({
					url:"/pages/activity/comment?id="+this.id+"&title="+this.title
				})
			},
			// 发表评论
			sendCom(){
				http({
					url:"/prod-api/api/activity/comment",
					method:"POST",
					data:{
						activityId:this.id,
						content:this.com
					}
				}).then(res=>{
					if(res.data.code==200){
						uni.showToast({
							title:"发表成功！"
						})
						this.com=""
						this.getComnum()
						this.getComlist()
					}else{
						uni.showToast({
							title:res.data.msg,
							icon:"none"
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
.detail{
	.title{
		padding: 20rpx;
		font-size: 48rpx;
		font-weight: bold;
		.join{
			float: right;
			background-color: #000;
			color: #fff;
			border-radius: 20rpx;
			margin-left: 20rpx;
			margin-bottom: 20rpx;
		}
	}
	.info{
		clear: both;
		background-color: #f8f8f8;
		color: #ccc;
		border-radius: 20rpx;
		display: flex;
		justify-content: space-between;
		padding: 20rpx;
		margin: 20rpx;
		font-size: 26rpx;
	}
	.content{
		padding: 20rpx;
		img{
			width: 100%;
			border-radius: 20rpx;
		}
	}
	.recom{
		padding: 20rpx;
		.recomTop{
			padding: 20rpx;
			background-color: #ccc;
			font-weight: bold;
			border-radius: 20rpx 20rpx 0 0;
		}
		.recomList{
			padding: 20rpx;
			background-color: #ecf0f1;
			border-bottom: 1px solid #ccc;
		}
		.recomList:last-child{
			border-bottom: 0;
			border-radius: 0 0 20rpx 20rpx;
		}
	}
	.commentList{
		padding: 20rpx;
		.comNum{
			padding: 20rpx;
			font-weight: bold;
			border-radius: 20rpx;
			background-color: #ccc;
		}
		.noCom{
			padding: 20rpx;
			border-radius: 20rpx;
			background-color: #f8f8f8;
			margin: -10rpx 0 10rpx;
			text-align: center;
			color: #ccc;
			font-size: 26rpx;
		}
		.comList{
			padding: 10rpx 20rpx;
			border-radius: 20rpx;
			background-color: #f8f8f8;
			margin: 10rpx 0;
			.name{
				font-weight: bold;
			}
			.time{
				text-align: right;
				color: #ccc;
				font-size: 26rpx;
			}
		}
		.comList:last-child{
			margin-bottom: 0;
		}
		.allcom{
			background-color: #f8f8f8;
			text-align: center;
			color: #ccc;
			border-radius: 20rpx;
			font-size: 26rpx;
			padding: 20rpx;
		}
	}
	.comment{
		padding: 20rpx;
		border-top: 1px solid #ccc;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background-color: #fff;
		
		position: fixed;
		bottom: 0;
		width: 100%;
		height: 120rpx;
		box-sizing: border-box;
		input{
			flex: 1;
			background-color: #f8f8f8;
			border: 1px solid #ccc;
			padding: 10rpx 20rpx;
			border-radius: 15rpx;
			box-sizing: border-box;
			height:80rpx;
			font-size: 30rpx;
		}
		button{
			height: 80rpx;
			line-height: 80rpx;
			font-size: 30rpx;
			margin: 0;
			margin-left: 20rpx;
		}
	}
	.no{
		height: 120rpx;
	}
}
</style>